package com.yw.thesimpllestplayer.audioplayer;

import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioTrack;
import android.media.MediaCodec;
import android.media.MediaFormat;
import android.util.Log;

import java.nio.ByteBuffer;

/**
 * @ProjectName: TheSimpllestplayer
 * @Package: com.yw.thesimpllestplayer.audioplayer
 * @ClassName: AudioPlayer
 * @Description: 音频播放器
 * @Author: wei.yang
 * @CreateDate: 2021/11/6 13:57
 * @UpdateUser: 更新者：wei.yang
 * @UpdateDate: 2021/11/6 13:57
 * @UpdateRemark: 更新说明：
 * @Version: 1.0
 */
public class AudioPlayer {
    private static final String TAG = "AudioPlayer";

    /**
     * 采样率
     */
    private int mSampleRate = -1;

    /**
     * 声音通道数量
     */
    private int mChannels = 1;

    /**
     * PCM采样位数
     */
    private int mPCMEncodeBit = AudioFormat.ENCODING_PCM_16BIT;

    /**
     * 音频播放器
     */
    private AudioTrack mAudioTrack = null;

    /**
     * 音频数据缓存
     */
    private short[] mAudioOutTempBuf = null;

    public AudioPlayer(MediaFormat format) {
        try {
            mChannels = format.getInteger(MediaFormat.KEY_CHANNEL_COUNT);
            mSampleRate = format.getInteger(MediaFormat.KEY_SAMPLE_RATE);
            if (format.containsKey(MediaFormat.KEY_PCM_ENCODING)) {
                mPCMEncodeBit = format.getInteger(MediaFormat.KEY_PCM_ENCODING);
            } else {
                //如果没有这个参数，默认为16位采样
                mPCMEncodeBit = AudioFormat.ENCODING_PCM_16BIT;
            }
        } catch (Exception e) {
            Log.e(TAG, e.getMessage());
        }
    }

    /**
     * 初始化audioplayer
     */
    public void initPlayer(){
        int channelType = AudioFormat.CHANNEL_OUT_MONO;
        if (mChannels == 1) {
            channelType = AudioFormat.CHANNEL_OUT_MONO;//单声道
        } else {
            channelType = AudioFormat.CHANNEL_OUT_STEREO;//双声道
        }
        //获取最小缓冲区
        int minBufferSize = AudioTrack.getMinBufferSize(mSampleRate, channelType, mPCMEncodeBit);
        mAudioOutTempBuf = new short[minBufferSize / 2];
        mAudioTrack = new AudioTrack(
                AudioManager.STREAM_MUSIC,//播放类型，音乐
                mSampleRate,//采用率
                channelType,//通道数
                mPCMEncodeBit,//采样位数
                minBufferSize,//缓冲区大小
                AudioTrack.MODE_STREAM//播放模式，数据流动态写入，static是一次性写入
        );
        mAudioTrack.play();
    }
    /**
     * 播放音频
     *
     * @param outputBuffer 音频数据缓冲区
     * @param bufferInfo   缓冲区信息
     */
    public void play(ByteBuffer outputBuffer, MediaCodec.BufferInfo bufferInfo) {
        if (mAudioOutTempBuf.length < bufferInfo.size / 2) {
            mAudioOutTempBuf = new short[bufferInfo.size / 2];
        }
        outputBuffer.position(0);
        outputBuffer.asShortBuffer().get(mAudioOutTempBuf, 0, bufferInfo.size / 2);
        //开始播放
        mAudioTrack.write(mAudioOutTempBuf, 0, bufferInfo.size / 2);
    }

    /**
     * 暂停
     */
    public void pause() {
        mAudioTrack.pause();
    }

    /**
     * 停止
     */
    public void stop() {
        mAudioTrack.stop();
    }

    /**
     * 销毁
     */
    public void release() {
        mAudioTrack.release();
    }


}
